﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SharpBolt.Basics;
using InductBase;
using SharpBolt.Graphics;
using SharpBolt.Time;
using System.IO;
using Newtonsoft.Json;
using System.ComponentModel;

namespace SharpBolt.Graphics.GraphicSources
{
    [JsonObject]
    [Editor(typeof(Editing.UISpriteSourceEditor), typeof(System.Drawing.Design.UITypeEditor))]
    public class SpriteSource: IGraphicSource
    {
        public Vec2 HotSpot{get; private set;}
        public string Name { get; protected set; }

        public class Frame
        {
            public SbBitmap Bitmap { get; set; }
            public long Milliseconds { get; set; }
            public Vec2i Shift { get; set; }
        }

        public class Sequence
        {
            public Sequence(string name)
            {
                Name = name;
                Frames = new List<Frame>();
            }
            public string Name { get; set; }
            public List<Frame> Frames { get; set; }
            public int Rank { get; set; }
        }
       
        internal List<Sequence> Sequences { get; private set; }        


        private SpriteSource() {
            Sequences = new List<Sequence>();           
        }

        public static SpriteSource LoadSprite(string name)
        {
            

            if (!Engine.Miscs.IsResourceExist(name + ".spr")) return null;

            SpriteSource sprite =  new SpriteSource();
            sprite.Name = name;
            string description = Engine.Miscs.LoadText(name + ".spr");

            var lines = description.Split(new char[]{'\n','\r'},StringSplitOptions.RemoveEmptyEntries);

            foreach (var line in lines)
            {
                string[] args = line.Split(new char[] { '\t', ' ' }, StringSplitOptions.RemoveEmptyEntries);

                if (args[0] == "s")
                {
                    var seq = new Sequence(args[1]);
                    sprite.Sequences.Add(seq);

                    if (args.Length > 2)
                        seq.Rank = Int32.Parse(args[2]);
                        
                }
                else if (args[0] == "f")
                {
                    sprite.Sequences[sprite.Sequences.Count - 1].Frames.Add(
                        new Frame()
                        {
                            Bitmap = Engine.GraphicRender.GetBitmap(name + @"\" + args[1]),
                            Milliseconds = long.Parse(args[2]),
                            Shift = Vec2i.Parse(args[3])
                        }
                        );
                }

                if (args[0] == "h")
                {
                    sprite.HotSpot = Vec2.Parse(args[1]);
                }
            }


            return sprite;
        }

        public void FreeSprite(string name)
        {

        }

        public int GetSequenceIndex(string sequenceName)
        {
            return Sequences.FindIndex(p => p.Name == sequenceName);
        }
        
        public void Draw(Painter painter, Transformation transformation, Color color)
        {
            painter.DrawBitmap(Sequences[0].Frames[0].Bitmap, transformation, color);
        }

        [JsonIgnore]
        public Vec2 Size
        {
            get
            {
                return this.Sequences[0].Frames[0].Bitmap.Size;
            }
        }

        public override string ToString()
        {
            return Name +" "+ Sequences.Count;
        }
    }
}
